package crypto;

import java.util.Random;
import java.io.*;

public class Crypto {
	private static final int row = 50;
	private static final int column = 80;
	
	public void Encrypt(String plainTextList) {
		String currentDirectory = System.getProperty("user.dir") + "/";
		String cipherTextDirectory = System.getProperty("user.dir") + "/cipherText/";
		File directory = new File(cipherTextDirectory);
		if (!directory.exists()) { directory.mkdir(); }
		try {
			for (String plainText : plainTextList.split(",")) {
				FileWriter fw = new FileWriter(cipherTextDirectory + plainText + ".txt");
				FileReader fr1 = new FileReader(currentDirectory + plainText + ".txt");
				FileReader fr2 = new FileReader(currentDirectory + "pad00.txt");
				for (int i = 0; i < (row * column); i++) {
					fw.write((byte)((((fr1.read()-32)+(fr2.read()-32))%95)+32));
				}
				fw.close();
				fr1.close();
				fr2.close();
			}
		}
		catch (Exception e) {
			System.err.println("ERROR: " + e);
		}
	}
	
	public void Decrypt(String cipherTextList) {
		String currentDirectory = System.getProperty("user.dir") + "/";
		String cipherTextDirectory = System.getProperty("user.dir") + "/cipherText/";
		String decryptedTextDirectory = currentDirectory + "decryptedText/";
		File directory = new File(decryptedTextDirectory);
		if (!directory.exists()) { directory.mkdir(); }
		try {
			for (String cipherText : cipherTextList.split(",")) {
				FileWriter fw = new FileWriter(decryptedTextDirectory + cipherText + ".txt");
				FileReader fr1 = new FileReader(cipherTextDirectory + cipherText + ".txt");
				FileReader fr2 = new FileReader(currentDirectory + "pad00.txt");
				for (int i = 0; i < (row * column); i++) {
					fw.write((byte)((((fr1.read()-32)-(fr2.read()-32))%95)+32));
				}
				fw.close();
				fr1.close();
				fr2.close();
			}
		}
		catch (Exception e) {
			System.err.println("ERROR: " + e);
		}
	}
	
	public void GeneratePads(String padList) {
		String padDirectory = System.getProperty("user.dir") + "/pads/";
		File directory = new File(padDirectory);
		if (!directory.exists()) { directory.mkdir(); }
		try {
			for (String pad : padList.split(",")) {
				FileWriter fw = new FileWriter(padDirectory + pad + ".txt");
				for (int i = 0; i < row; i++) {
					for (int j = 0; j < column-1; j++) {
						fw.write((byte)(new Random().nextInt(95) + 32));
					}
					fw.write(' ');
				}
				fw.close();
			}
		}
		catch (Exception e) {
			System.err.println("ERROR: " + e);
		}
	}
	
	public static void main(String[] args) {
		try {
			Crypto crypto = new Crypto();
			if (args[0].equals("-e")) {
				System.out.println("encryption method");
				crypto.Encrypt(args[1]);
			}
			else if (args[0].equals("-d")) {
				System.out.println("decryption method");
				crypto.Decrypt(args[1]);
			}
			else if (args[0].equals("-p")) {
				System.out.println("generate pads");
				crypto.GeneratePads(args[1]);
			}
			else if (args[0].equals("-r")) {
				// recover pads
				// stub
			}
		}
		catch (Exception e) {
			System.err.println("Error: Incorrect usage");
		}
	}
}